#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "userled.h"
#include "userport.h"

static RTC_HandleTypeDef hrtc;

RTC_DateTypeDef GetData;
RTC_TimeTypeDef GetTime; 
void acquisitionTime(RTC_TimeTypeDef * stime,RTC_DateTypeDef*sdate);

/********  LED GPIO 配置 *************************/
void LED_init(void)
{
		rt_pin_mode(RUN_GPIO_PORT, PIN_MODE_OUTPUT);
		rt_pin_mode(NET4G_GPIO_PORT, PIN_MODE_OUTPUT);
		rt_pin_mode(NET485_GPIO_PORT, PIN_MODE_OUTPUT);	
	  rt_pin_mode(NETLORA_GPIO_PORT, PIN_MODE_OUTPUT);
	  rt_pin_mode(POWER_4G_PORT, PIN_MODE_OUTPUT);
	  rt_pin_write(POWER_4G_PORT, PIN_HIGH);
}


/****************************************************************************/
/****************************************************************************/	 
void MX_RTC_Init(void)
{
  RTC_TimeTypeDef sTime = {0}; 
  RTC_DateTypeDef	sDate={0};
	char NTCtime[50];		
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  hrtc.Init.SynchPrediv = 255;
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  if (HAL_RTC_Init(&hrtc) != HAL_OK){
    Error_Handler();
  }
   rt_event_recv(EVENTDevic,EVENT_SocketConnect|EVENT_NTPOK,RT_EVENT_FLAG_AND,RT_WAITING_FOREVER,RT_NULL);
	 memset(NTCtime,0,sizeof(NTCtime));
	 acquisitionTime(&sTime,&sDate);
	 if(sDate.Year>10)
	 {
		   sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
			 sTime.StoreOperation = RTC_STOREOPERATION_RESET;
			 if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK){
					Error_Handler();
			 }
			 if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK){
					Error_Handler();
			 }
	 }

}


/****************************************************************************/
/****************************************************************************/
void acquisitionTime(RTC_TimeTypeDef * stime,RTC_DateTypeDef*sdate)
{
  time_t now=0;
	char *timebuff;
  char buff[50];

	int Tyear=0;
	int Tmonth=0;
	int Tdate=0;
	int Thour;
	int Tminute;
	int Tsecond;
	
	memset(buff,0,sizeof(buff));		
	now = time(RT_NULL);
	timebuff=ctime(&now);
	sscanf(timebuff, "%[^ ] %[^ ] %[^ ] %[^ ] %[^ ]",&buff[0],&buff[5],&buff[10],&buff[15],&buff[25]);
	Tyear=atoi(&buff[27]);
  Tdate=atoi(&buff[10]);
	sscanf(&buff[15], "%d:%d:%d",&Thour,&Tminute,&Tsecond);
  if(strncmp(&buff[5], "Jan", 3)==0){
		Tmonth=1;
	}
	else if(strncmp(&buff[5], "Feb", 3)==0){
		Tmonth=2;
	}
	else if(strncmp(&buff[5], "Mar", 3)==0){
		Tmonth=3;
	}
	else if(strncmp(&buff[5], "Apr", 3)==0){
		Tmonth=4;
	}
	else if(strncmp(&buff[5], "May", 3)==0){
		Tmonth=5;
	}
	else if(strncmp(&buff[5], "Jun", 3)==0){
		Tmonth=6;
	}
	else if(strncmp(&buff[5], "Jul", 3)==0){
		Tmonth=7;
	}
	else if(strncmp(&buff[5], "Aug", 3)==0){
		Tmonth=8;
	}
	else if(strncmp(&buff[5], "Sep", 3)==0){
		Tmonth=9;
	}
	else if(strncmp(&buff[5], "Oct", 3)==0){
		Tmonth=10;
	}
	else if(strncmp(&buff[5], "Nov", 3)==0){
		Tmonth=11;
	}
	else if(strncmp(&buff[5], "Dec", 3)==0){
		Tmonth=12;
	}
	else{
		Tmonth=0;
	}
	stime->Hours=Thour;
  stime->Minutes=Tminute;	
	stime->Seconds=Tminute;
	sdate->Year=Tyear;
	sdate->Month=Tmonth;
	sdate->Date=Tdate;
}


void getrcttime(char *pttime)
{
	int Tyear=0;
	int Tmonth=0;
	int Tdate=0;
	int Thour;
	int Tminute;
	int Tsecond;
	
	HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN);
	HAL_RTC_GetDate(&hrtc, &GetData, RTC_FORMAT_BIN);
	Tyear=GetData.Year;
	Tmonth=GetData.Month;
	Tdate=GetData.Date;
	Thour=GetTime.Hours;
	Tminute=GetTime.Minutes;
	Tsecond=GetTime.Seconds;
  sprintf(pttime, "%.2d%.2d%.2d%.2d%.2d%.2d",Tyear,Tmonth,Tdate,Thour,Tminute,Tsecond);
}  

 
	